//添加小车模型
var path;
var carEntity;
function addentity() {
    console.log(window._viewer);
    carEntity = window._viewer.entities.add({
        id: '1',
        workingCondition: '正在工作',
        position: Cesium.Cartesian3.fromDegrees(119.06511782986772, 45.895596882847872),
        model: {
            uri: './data/xiaofangche.gltf',
            scale: 1.0,
            minimumPixelSize: 70,
            maximumScale: 70
        }
    });

    window._viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {
        // 从屏幕坐标中获取地球上的坐标
        var pickedObject = window._viewer.scene.pick(movement.position);
        if (Cesium.defined(pickedObject) && pickedObject.id === carEntity) {
            // 创建一个HTML弹窗
            var popup = document.createElement('div');
            popup.style.cssText = 'position:absolute;top:50%;left:50%;transform: translate(-50%, -50%);z-index: 100;background-color: white;padding: 20px;border-radius: 10px;box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);';
            var popuptitle = document.createElement('h2');
            popuptitle.innerHTML = '小车信息';
            popup.appendChild(popuptitle);
            // 创建弹窗中的内容

            var popupContent = document.createElement('div');
            popupContent.innerHTML = '这是一辆小车，它的工作状态时是 (' + carEntity.workingCondition + ')，车牌号为 ' + carEntity.id;
            popup.appendChild(popupContent);

            // 创建一个关闭按钮
            var closeButton = document.createElement('button');
            closeButton.innerHTML = '显示车辆运行轨迹';
            closeButton.onclick = function () {
                // 将弹窗从DOM元素中移除
                popup.parentNode.removeChild(popup);
                setTimeout(function () {
                    moveCar();
                }, 1000);
            };
            popup.appendChild(closeButton);

            // 将弹窗添加到Cesium Viewer的DOM元素中
            window._viewer.container.appendChild(popup);
        }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}



function locateEntity() {
    var entityId = document.getElementById('searchInput').value;
    if (entityId == '车辆') {
        window._viewer.flyTo(carEntity);
    }
    hideSearchPopup();
}

function moveCar() {
    // 创建路径实体
    var lujingdata = [
        [119.06511782986772, 45.895596882847872, 0],
        [119.15034266886764, 45.9698186561991, 0],
    ];
    path = window._viewer.entities.add({
        name: 'line',
        polyline: {
            positions: Cesium.Cartesian3.fromDegreesArrayHeights(lujingdata.flat()),
            material: Cesium.Color.RED,
            width: 10
        }
    });

    // 创建 SampledPositionProperty 对象，用于存储路径的位置信息
    var property = new Cesium.SampledPositionProperty();
    var startTime = new Date();
    var stopTime;
    var timestamp = startTime.getTime();

    lujingdata.forEach((pos, index) => {
        var time = new Date(timestamp + index * 5000);
        stopTime = time;
        var position = Cesium.Cartesian3.fromDegrees(...pos);
        property.addSample(Cesium.JulianDate.fromDate(time), position);
    });
    property.setInterpolationOptions({
        interpolationDegree: 0.00000001,
        interpolationAlgorithm: Cesium.LagrangePolynomialApproximation,
    });

    // 将位置信息绑定到小车实体上
    carEntity.position = property;

    // 启动动画
    viewer.clock.currentTime = Cesium.JulianDate.fromDate(startTime); // 修改时间轴的当前时间
    viewer.clock.stopTime = Cesium.JulianDate.fromDate(stopTime);
    viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP;
    viewer.clock.multiplier = 0.01; // 播放速度
    viewer.clock.shouldAnimate = true; // 开始播放
}









